﻿@using SuperDump.Models
@model SuperDumpService.ViewModels.ReportViewModel

<div class="panel panel-info">
	<div class="panel-heading">Summary</div>
	<div class="panel-body">
		<!--anomalies-->
		@{bool noAnomalies = true;}

		@if (!string.IsNullOrEmpty(Model.AnalysisError)) {
			noAnomalies = false;
			<h5>Dump Analysis failed with errors, dumpproc log was analyzed instead</h5>
			<pre class="pre-small">Error: @Model.AnalysisError</pre>
		}

		@if (Model.DumpType == SuperDumpService.Models.DumpType.LinuxCoreDump) {
			<h5>Process was started with: <b>@((Model.Result.SystemContext as SuperDump.Models.SDCDSystemContext)?.Args)</b></h5>
		}
		@if (Model.Result.LastEvent != null) {
			noAnomalies = false;
			<h4>LastEvent:</h4>
			<table class="table table-bordered">
				<thead>
					<tr>
						@if (Model.DumpType == SuperDumpService.Models.DumpType.WindowsDump) {
							<th>Engine thread ID</th>
							<th>Type</th>
							<th>Description</th>
						} else {
							<th>Thread ID</th>
							<th>Signal</th>
							<th>Description</th>
						}
					</tr>
				</thead>
				<tbody>
					<tr>
						<td>
							@{ var lastEventThreads = @Model?.Result?.ThreadInformation?.Where(x => x.Value.EngineId == Model.Result.LastEvent.ThreadId); }
							@if (lastEventThreads != null) {
								@if (lastEventThreads.Count() == 0) {
									<span>#@Model.Result.LastEvent.ThreadId (no corresponding thread found)</span>
								} else {
									<a class="thread-link" href="#@lastEventThreads.Single().Value.OsId">#@Model.Result.LastEvent.ThreadId</a>
								}
							}
						</td>
						<td>@Model.Result.LastEvent.Type</td>
						<td>@Model.Result.LastEvent.Description</td>
					</tr>
				</tbody>
			</table>
		}

		@if (Model.Result.DeadlockInformation != null) {
			@if (Model.Result.DeadlockInformation.Count > 0) {
				noAnomalies = false;
				<h4>Deadlock detection:</h4>
				@foreach (var d in Model.Result.DeadlockInformation) {
					<p>
						Thread
						<a href="#@d.lastThreadId" class="thread-link">#@Model.Result.ThreadInformation.Values.Single(x => x.OsId == d.lastThreadId).EngineId</a> waits for Thread
						<a href="#@d.lockedOnThreadId" class="thread-link">@d.lockedOnThreadId</a>
					</p>
				}}
		}
		@if (Model.Result.ExceptionRecord != null) {
			@if (Model.Result.ExceptionRecord.Count > 0) {
				noAnomalies = false;
				<h4>Exception report:</h4>
				<table class="table table-bordered">
					<thead>
						<tr>
							<th>Engine thread ID</th>
							<th></th>
						</tr>
					</thead>
					<tbody>
						@foreach (var ex in Model.Result.ExceptionRecord) {
							<tr>
								@if (Model.Result.ThreadInformation.TryGetValue((uint)ex.OSThreadId, out SDThread threadInfo)) {
									<td><a class="thread-link" href="#@ex.OSThreadId">#@threadInfo.EngineId</a></td>
								} else {
									<td>#@ex.OSThreadId.ToString("X")</td>
								}
								<td>
									<p><strong>Type: </strong>@ex.Type</p>
									<p><strong>Message: </strong>@ex.Message</p>
									<p>
										<strong>Stacktrace:</strong>
									</p>
									@if (ex.StackTrace != null && ex.StackTrace.Count > 0) {

										<p>@Html.Partial("_StacktracePlain", @ex.StackTrace)</p>

									} else {
										<p>Not avaliable</p>
									}
								</td>
							</tr>
						}
					</tbody>
				</table>
			}
		}
		@if (noAnomalies) {
			<h4>No special anomalies found</h4>
		}
	</div>
</div>